package com.gxgeek.javabasic.activemq.queue;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by gx on 2017/7/13.
 */
@Slf4j
public class AppCustomer {
    private static final String url = "tcp://127.0.0.1:61616";
    private static final String qururName = "queue-test";
    public static void main(String[] args) throws JMSException {
        //创建连接工厂

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //1.创建连接
        Connection connection = connectionFactory.createConnection();
        //2.启动连接
        connection.start();
//            3.                                       是否使用事务           应答模式
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //4.创建目标
        Destination destination = session.createQueue(qururName);


        //5.创建消费者
        MessageConsumer consumer = session.createConsumer(destination);

        //创建监听器
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    log.info("接受消费-------> {}", textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
//        最后关闭连接
//        session.close();
//        connection.close();
//
    }

}
